package com.quan.server.service;

import com.mark.util.CommonUtil;
import com.quan.server.Const;
import com.quan.server.beans.ConfigItem;
import com.quan.server.helper.MyAppHelper;
import com.quan.server.jpa.ConfigItemRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

@Component
@Path("/configItems")
public class ConfigItemService {
    private static Logger logger = LoggerFactory.getLogger(ConfigItemService.class);
    @Autowired
    private ConfigItemRepository configItemRepository;

    @Autowired
    private MyAppHelper myAppHelper;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response post(ConfigItem configItem) {
        try {
            configItem.id = CommonUtil.createRandomId();
            configItem = configItemRepository.save(configItem);
            return Response.status(Response.Status.CREATED).entity(configItem).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @DELETE
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response delete(@PathParam("id") String id) {
        try {
            configItemRepository.delete(id);
            return Response.status(Response.Status.NO_CONTENT).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response put(ConfigItem configItem) {
        try {
            if (CommonUtil.isEmpty(configItem.id)) {
                configItem.id = CommonUtil.createRandomId();
            }
            configItem = configItemRepository.save(configItem);
            return Response.ok(configItem).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response get(@PathParam("id") String id) {
        try {
            ConfigItem configItem = configItemRepository.findOne(id);
            if (configItem == null) {
                return Response.status(Response.Status.NO_CONTENT).build();
            } else {
                return Response.ok(configItem).build();
            }
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAll() {
        try {
            List<ConfigItem> configItemList = configItemRepository.findAll();
            return Response.ok(configItemList).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("branch")
    public Response post(List<ConfigItem> configItemList) {
        for (ConfigItem ConfigItem : configItemList) {
            post(ConfigItem);
        }
        return Response.ok().build();
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/setDefaultTaobaoAccountConfiguration")
    public Response setDefaultTaobaoAccountConfiguration(Map<String, String> configuration) {
        try {
            myAppHelper.setDefaultTaobaoAccountConfiguration(configuration);
            return Response.ok().build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Path("/getDefaultTaobaoAccountConfiguration")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getDefaultTaobaoAccountConfiguration() {
        try {
            List<String> keyList = new Vector<>();
            keyList.add(Const.KeyDefaultTaobaoAccountAppKey);
            keyList.add(Const.KeyDefaultTaobaoAccountAppSecret);
            Map<String, String> configuration = myAppHelper.getConfiguration(keyList);
            return Response.ok(configuration).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/setShareConfiguration")
    public Response setShareConfiguration(Map<String, String> configuration) {
        try {
            myAppHelper.setConfiguration(configuration);
            return Response.ok().build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Path("/getShareConfiguration")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getShareConfiguration() {
        try {
            List<String> keyList = new Vector<>();
            keyList.add(Const.KeyShareA);
            keyList.add(Const.KeyShareB);
            keyList.add(Const.KeyShareC);
            Map<String, String> configuration = myAppHelper.getConfiguration(keyList);
            return Response.ok(configuration).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }
}
